#include "stdafx.h"
#include <MyViews/PanTool.h>
#include <MyViews/PanView.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const int PANNING_BENCH_SCROLL_LINE = 5;
/////////////////////////////////////////////////////////////////////////////
// PanTool

IMPLEMENT_DYNAMIC(PanTool, Tool)

PanTool::PanTool(PanView & owner)
	: m_Owner(owner)
{
	m_Panning = false;

	VERIFY (m_CursorHand = ::AfxGetApp()->LoadCursor("PanTool_CursorHand"));
	VERIFY (m_CursorHandDrag = ::AfxGetApp()->LoadCursor("PanTool_CursorHandDrag"));
}

PanTool::~PanTool()
{

}


void PanTool::Show()
{

}

void PanTool::Hide()
{
	m_Panning = false;
}

BEGIN_MESSAGE_MAP(PanTool, CCmdTarget)
	//{{AFX_MSG_MAP(PanTool)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// PanTool message handlers
							   
void PanTool::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	CPoint p = m_Owner.GetOrigin();
	switch (nChar) {
	case VK_LEFT:
		p.x -= PANNING_BENCH_SCROLL_LINE;
		break;
	case VK_RIGHT:
		p.x += PANNING_BENCH_SCROLL_LINE;
		break;
	case VK_UP:
		p.y -= PANNING_BENCH_SCROLL_LINE;
		break;
	case VK_DOWN:
		p.y += PANNING_BENCH_SCROLL_LINE;
		break;
	case VK_HOME:
		p.x = 0;
		break;
	case VK_END:
		p.x = m_Owner.GetBenchSize().cx;
		break;
	case VK_PRIOR:
		p.y -= m_Owner.GetClientSize().cy;
		break;
	case VK_NEXT:
		p.y += m_Owner.GetClientSize().cy;
	}
	m_Owner.SetOrigin(p);
}

void PanTool::OnLButtonDown(UINT nFlags, CPoint p)
{
	m_Panning = true;

	m_Start = p;
	m_Old = m_Owner.GetOrigin();

	m_Owner.SetCapture();

	m_Owner.SetFocus();
	m_Owner.UpdateCursor();
}

void PanTool::OnLButtonUp(UINT nFlags, CPoint p)
{
	m_Panning = false;

	ReleaseCapture();

	m_Owner.UpdateCursor();
}

void PanTool::OnMouseMove(UINT nFlags, CPoint p)
{
	if (m_Panning) {
		m_Owner.SetOrigin(m_Start - p + m_Old);
	}
}

bool PanTool::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{	
	if (nHitTest != HTCLIENT && nHitTest != 0)
		return false;

	if (m_Panning)
		SetCursor(m_CursorHandDrag);
	else
		SetCursor(m_CursorHand);

	return true;
}
